home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
public
/
bit
/
src
/
ulib
/
wildmat.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
2KB
|
81 lines
/*
* adapted from FORMS library routines which were in turn
* adapted from Rich Salz
*. Copyright(c) 1993,1994 by T.C. Zhao
* All rights reserved.
*.
*/
#include <stdio.h>
#include <string.h>
#ifndef FALSE
#define FALSE 0
#define TRUE 1
#endif
#include "ulib.h"
static int match_star(const char *s, const char *p);
static int
do_matching(register const char *s, register const char *p)
/* match string "s" to pattern "p" */
{
register int last, matched, reverse;
for (; *p; s++, p++)
{
if (*s == '\0')
return (*p == '*' && *++p == '\0' ? TRUE : -1);
switch (*p)
{ /* parse pattern */
case '\\': /* Literal match with following character. */
if (*s != *++p)
return FALSE;
continue;
default: /* literal match */
if (*s != *p)
return FALSE;
continue;
case '?': /* Match anything. */
continue;
case '*': /* Trailing star matches everything. */
return (*++p ? match_star(s, p) : TRUE);
case '[': /* [!....] means inverse character class. */
if ((reverse = p[1] == '!'))
p++;
for (last = 0400, matched = FALSE; *++p && *p != ']'; last = *p)
/* This next line requires a good C compiler. */
/* range? (in bounds) (equal) */
if ((*p == '-') ? (*s <= *++p && *s >= last) : (*s == *p))
matched = TRUE;
if (matched == reverse)
return FALSE;
continue;
}
}
return *s == '\0';
}
static int
match_star(const char *s, const char *p)
{
int result;
while ((result = do_matching(s, p)) == FALSE) /* gobble up * match */
if (*++s == '\0')
return -1;
return result;
}
int
wildmat(const char *s, const char *p)
{
if (*p == '\0' && *s != '.')
return TRUE;
else if (*p == '\0')
return FALSE;
else if ((*p == '?' || *p == '*') && *s == '.')
return FALSE;
else
return do_matching(s, p) == TRUE;
}